package com.ruoyi.custom.kami.service.impl;

import java.awt.image.Kernel;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.custom.kami.utils.BuildKaMiTools;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.custom.kami.mapper.CmsKamiMapper;
import com.ruoyi.custom.kami.domain.CmsKami;
import com.ruoyi.custom.kami.service.ICmsKamiService;

/**
 * listService业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Service
public class CmsKamiServiceImpl implements ICmsKamiService {
    @Autowired
    private CmsKamiMapper cmsKamiMapper;

    /**
     * 查询list
     *
     * @param id list主键
     * @return list
     */
    @Override
    public CmsKami selectCmsKamiById(Long id) {
        return cmsKamiMapper.selectCmsKamiById(id);
    }

    /**
     * 查询list列表
     *
     * @param cmsKami list
     * @return list
     */
    @Override
    public List<CmsKami> selectCmsKamiList(CmsKami cmsKami) {
        return cmsKamiMapper.selectCmsKamiList(cmsKami);
    }

    /**
     * 新增list
     *
     * @param cmsKami list
     * @return 结果
     */
    @Override
    public int insertCmsKami(CmsKami cmsKami) {
        cmsKami.setCreateTime(DateUtils.getNowDate());
        return cmsKamiMapper.insertCmsKami(cmsKami);
    }


    /**
     * 修改list
     *
     * @param cmsKami list
     * @return 结果
     */
    @Override
    public int updateCmsKami(CmsKami cmsKami) {
        return cmsKamiMapper.updateCmsKami(cmsKami);
    }

    /**
     * 批量删除list
     *
     * @param ids 需要删除的list主键
     * @return 结果
     */
    @Override
    public int deleteCmsKamiByIds(Long[] ids) {
        return cmsKamiMapper.deleteCmsKamiByIds(ids);
    }

    /**
     * 删除list信息
     *
     * @param id list主键
     * @return 结果
     */
    @Override
    public int deleteCmsKamiById(Long id) {
        return cmsKamiMapper.deleteCmsKamiById(id);
    }

    @Override
    public int buildCarMi(CmsKami cmsKami) {
        int count = 10;
        int length = 16;
        Set<String> setKami = BuildKaMiTools.buildKaimi(length, count);
        Iterator<String> it = setKami.iterator();
        int tureCount = 0;
        while (it.hasNext()) {
            String str = it.next();
            CmsKami kami = new CmsKami();
            kami.setCardPasswd(str);
            List<CmsKami> list = selectCmsKamiList(kami);
            if (list != null && !list.isEmpty()) {
              continue;
            }
            kami.setKehu(cmsKami.getKehu());
            kami.setEnUse(cmsKami.getEnUse());
            kami.setCreateTime(DateUtils.getNowDate());
            kami.setEnable(1L);
            cmsKamiMapper.insertCmsKami(kami);
            tureCount++;
        }
        return tureCount;
    }

    @Override
    public CmsKami selectKaMiInfoByKamistr(CmsKami cmsKami) {
        List<CmsKami> list = this.selectCmsKamiList(cmsKami);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }
}
